package com.tieba.controller;


import com.tieba.pojo.*;
import com.tieba.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/messages")//共同的请求路径
public class MessageController {

    @Autowired
    private MessageService messageService;


    //查询所有帖子，没有被评论id的留言就是帖子
    @GetMapping
    public Result list(){
        log.info("查询所有帖子");
        List<Message> list = messageService.list();
        return Result.success(list);
    }

    //根据留言id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id为{}的帖子",id);
        Message message = messageService.getById(id);
        return Result.success(message);
    }

    //根据用户id查询帖子
    @GetMapping("/user/{id}")
    public Result listByOwerId(@PathVariable Integer id){
        List<Message> list = messageService.listByOwerId(id);
        return Result.success(list);
    }

    //根据被评论id查询帖子下有哪些评论
    @GetMapping("/commented/{id}")
    public Result listByCommenteId(@PathVariable Integer id){
        List<Message> list = messageService.listByCommentedId(id);
        return Result.success(list);
    }

    //新增帖子
    @PostMapping
    public Result add(@RequestBody Message message){
        log.info("新增留言:{}",message);
        messageService.add(message);
        return Result.success();
    }
    //删除留言  批量删除
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除帖子，ids:{}",ids);
        messageService.delete(ids);
        return Result.success();
    }

    //修改留言信息  当点赞，评论，收藏时，更新该留言的信息
//    @PutMapping
//    public Result update(@RequestBody Message message){
//        log.info("更新点赞/评论/收藏数message：{}",message);
//        messageService.update(message);
//        return Result.success();
//    }

    //查看用户点赞了哪些留言
    @GetMapping("/like/{userId}")
    public Result listLike(@PathVariable Integer userId){
        log.info("查看用户{} 点赞的评论",userId);
        List<Message> list = messageService.listLike(userId);
        return Result.success(list);
    }

    //查看哪些用户点赞我的帖子
    @GetMapping("/liked/{messageId}")
    public Result listLiked(@PathVariable Integer messageId){

        List<Integer> UserIds = messageService.listLikedUsers(messageId);
        log.info("查看评论{} 被哪些用户点赞了,用户{}：",messageId,UserIds.size());
        return Result.success(UserIds);
    }

    //查看用户评论了哪些帖子或者评论
    @GetMapping("/comment/{userId}")
    public Result listComment(@PathVariable Integer userId){
        log.info("用户{} 评论的评论：",userId);
        List<Message> list = messageService.listComment(userId);
        return Result.success(list);
    }


    //查看用户收藏了哪些留言
    @GetMapping("/collection/{userId}")
    public Result listCollection(@PathVariable Integer userId){
        log.info("用户{} 收藏的评论：",userId);
        List<Message> list = messageService.listCollection(userId);
        return Result.success(list);
    }

    //查看哪些用户收藏了我的帖子
    @GetMapping("/collected/{messageId}")
    public Result listCollected(@PathVariable Integer messageId){

        List<Integer> UserIds = messageService.listCollectedUsers(messageId);
        log.info("查看评论{} 被哪些用户收藏了，用户{}：",messageId,UserIds);
        return Result.success(UserIds);
    }

    //点赞了一条评论
    @PostMapping("/like")
    public Result like(@RequestBody Like like){
        log.info("用户{} 给留言 {} 点了一个赞",like.getUserId(),like.getMessageId());
        messageService.addlike(like);
        return Result.success();
    }

    //评论不需要单独写，用新增帖子实现即可
//    @PostMapping("/comment")
//    public Result comment(@RequestBody Message message){
//        log.info("用户{} 对留言 {} 发表了评论",message.getUserId(),message.getCommentedId());
//        log.info("评论内容:{}",message);
//        messageService.add(message);
//        Comment comment = new Comment();
//        comment.setUserId(message.getUserId());
//        comment.setMessageId(message.getCommentedId());
//        Integer commentId = messageService.getByUserIdAndCommentedId(message.getUserId(),message.getCommentedId());
//        comment.setCommentId(commentId);
//        log.info("存储评论：{}",comment);
//        messageService.addcomment(comment);
//        return Result.success();
//    }

    //收藏了一条评论
    @PostMapping("/collection")
    public Result collection(@RequestBody Collection collection){
        log.info("用户{} 收藏了留言 {}",collection.getUserId(),collection.getMessageId());
        messageService.addcollection(collection);
        return Result.success();
    }


    //取消点赞
    @PostMapping("/unlike")
    public Result unLike(@RequestBody Like like){
        log.info("用户{} 给评论 {} 取消了点赞",like.getUserId(),like.getMessageId());
        messageService.deleteLike(like);
        return Result.success();
    }

    //取消收藏
    @PostMapping("/uncollection")
    public Result unCollection(@RequestBody Collection collection){
        log.info("用户{} 对评论 {} 取消了收藏",collection.getUserId(),collection.getMessageId());
        messageService.deleteCollection(collection);
        return Result.success();
    }
}
